package com.stgy.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.FacilityInfo;
import com.stgy.lease.model.domain.LabelInfo;
import com.stgy.lease.model.enums.FacilityInfoTypeEnum;
import com.stgy.lease.model.enums.LabelInfoTypeEnum;
import com.stgy.lease.web.admin.service.LabelInfoService;
import com.stgy.lease.web.admin.mapper.LabelInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lingx
 * @description 针对表【label_info(标签信息表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
public class LabelInfoServiceImpl extends ServiceImpl<LabelInfoMapper, LabelInfo>
        implements LabelInfoService {

    @Override
    public List<LabelInfo> listByEnum(LabelInfoTypeEnum typeEnum) {
        LambdaQueryWrapper<LabelInfo> wrapper = new LambdaQueryWrapper<>();
        if (typeEnum != null) {
            wrapper.eq(LabelInfo::getType, typeEnum.getCode());
        }
        return this.list(wrapper);
    }

    //新增或者更新配套信息业务
    @Override
    public boolean saveOrUpdateLabelInfo(LabelInfo labelInfo) {
        if (labelInfo == null || labelInfo.getName() == null) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }
        LambdaQueryWrapper<LabelInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabelInfo::getType, labelInfo.getType().getCode());
        wrapper.eq(LabelInfo::getName, labelInfo.getName());
        LabelInfo one = this.getOne(wrapper);
        //唯一校验 type+name 组合唯一
        if (one != null && !one.getId().equals(labelInfo.getId())) {
//        if (one != null && one.getId().longValue() != facilityInfo.getId()) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }

        return this.saveOrUpdate(labelInfo);
    }
}




